package com.mnxiaotu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mnxiaotu.entity.Menu;
import com.mnxiaotu.service.MenuService;
import com.mnxiaotu.mapper.MenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 86182
* @description 针对表【menu】的数据库操作Service实现
* @createDate 2024-04-25 23:58:41
*/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu>
    implements MenuService{

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> buildTreeMenu(List<Menu> menuList) {
        List<Menu> resultMenuList=new ArrayList<>();

        for(Menu menu:menuList){

            // 寻找子节点
            for(Menu e:menuList){
                if(e.getParentId().equals(menu.getId())){
                    menu.getChildren().add(e);
                }
            }

            if(menu.getParentId()==0L){
                resultMenuList.add(menu);
            }
        }

        return resultMenuList;
    }

    @Override
    public void deleteById(Long id) {
        menuMapper.deleteById(id);
    }

}




